#coding=utf-8

import web
import time
from pages.Another import AnotherClass01


render = web.template.render('templates', globals={'ver': 'leeking'})
urls = (
    '/', 'Index',
    # '/i/(\D+)_(\d+)_(\w+)', 'MyRegex',  # pattern 1 for test
    '/i/(\w+)_(\w+)_(\w+)', 'MyRegex',  # pattern 2 for test
    '/job/\d{1,}', 'Job',
    '/ppp', 'AnotherClass01'
)



class Index:
    def GET(self):
        args = web.input(account="me", money=None)
        print args
        db_mode = {'file': True, 'memory': False}
        db_file = "webpy.db" if db_mode['file'] else ":memory:"
        db = web.database(dbn='sqlite', db=db_file)
        cur = db._db_cursor()
        if True:
            cur.execute('CREATE TABLE IF NOT EXISTS bank_income(i LONG PRIMARY KEY, account TEXT, money REAL)')

        count = db.select('bank_income')
        if len(list(count)) <= 0:
            for i in xrange(5):
                db.insert('bank_income', i=int(time.time()*1000), account=args['account'], money=args['money'])
                time.sleep(0.1)

        '''select'''
        vars = {'account': 'me', 'm': '%'}
        v = db.select('bank_income', vars, where='account = $account and money like $m')
        out = render.index(args, v)
        print "****************", out.myvar
        return out


class MyRegex:
    def GET(self, name, age, status):
        # lst = []
        # lst.append(web.input(name=None))
        # lst.append(web.input(age=None))
        # args = {'name': lst[0].name, 'age': lst[1].age}
        print "name is {0}, and age is {1}".format(name, age, status)
        args = {'name': name, 'age': age, 'status': status}
        return render.myregex_demo(args)
        # return "Web Hello, world 你好!"


class Job:
    def GET(self):
        # return "I'm going to find a very good job and make a big money!"
        a = "this is a"
        b = "this is b"
        suffix = 2
        return render.job(a, b, suffix)


def hello_regex(phone):
    import re
    # r = re.compile(r'\b0(\d){2,4}-(\d){7,9}\b')
    r = re.compile(r'[\u4e00-\u9fa5]')
    if not phone:
        raise Exception("phone cannot be none")
    for i in phone:
        print "testing [{0}]".format(i),
        m = r.search(i)
        if m:
            print "MATCHED at [{pos}] [{0}]".format(m.group(), pos=m.span())
        else:
            print 'no match ->', i


def gen_list():
    return ['yes- *hi*33af iaa   kf z', ' hi#  there!', 'him',
            ' hi', 'yes- hi*33af ia   x你好kf', 'god ^hello# y']


if __name__ == "__main__":
    # phone = gen_list()
    # hello_regex(phone)
    print '######### %s' % globals()
    app = web.application(urls, globals())
    app.run()
